package com.procedures;


import java.io.File;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import com.util.Util;

public class EnterJdbcDao {
	
	com.PublicSystem sys=com.PublicSystem.getInstance();
	private String authorName = "gjx";//作者名字

	private String[] colnames; // 列名数组
	private String[] colTypes; //列名类型数组
	private int[] colSizes; //列名大小数组
	private boolean f_util = false; // 是否需要导入包java.util.*
	private boolean f_sql = false; // 是否需要导入包java.sql.*
    
	


	public String createVo(String tablename,String packageOutPath,String index){
    	
    	Connection con=null;String res="0";
		//查要生成实体类的表
    	String sql = "select * from " + tablename;
    	Statement pStemt = null;
    		try { 

    		con=sys.getConnection();
    		pStemt = (Statement) con.createStatement();  
    	    ResultSet rs = pStemt.executeQuery(sql);  
    		ResultSetMetaData rsmd = rs.getMetaData();  

			int size = rsmd.getColumnCount();	//统计列
			colnames = new String[size];
			colTypes = new String[size];
			colSizes = new int[size];
			for (int i = 0; i < size; i++) {
				colnames[i] = rsmd.getColumnName(i + 1);
				colTypes[i] = rsmd.getColumnTypeName(i + 1);
				
				if(colTypes[i].equalsIgnoreCase("date") || colTypes[i].equalsIgnoreCase("timestamp")){
					f_util = true;
				}
				if(colTypes[i].equalsIgnoreCase("blob") || colTypes[i].equalsIgnoreCase("char")){
					f_sql = true;
				}
				colSizes[i] = rsmd.getColumnDisplaySize(i + 1);
			}
			
			String content = parse(colnames,colTypes,colSizes,packageOutPath,tablename);
			
			try {
				//File directory = new File("");
				//String outputPath = directory.getAbsolutePath().replaceAll("\\\\", "/")+ "/src/"+packageOutPath.replace(".", "/")+"/"+initcap(tablename) + ".java";
				String outputPath = com.Const.path+"/src/"+packageOutPath.replace(".", "/")+"/"+Util.initcap(tablename) + ".java";
				
				FileWriter fw = new FileWriter(outputPath);
				PrintWriter pw = new PrintWriter(fw);
				pw.println(content);
				pw.flush();
				pw.close();
				System.out.println("在"+outputPath+"VO类已经创建成功");
				
				
				
				
			} catch (IOException e) {
				e.printStackTrace();
				res="1";
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			res="1";
		} finally{
			try
			{
				pStemt.close();
				con.close();
			}
			catch (SQLException e) {
				e.printStackTrace();
			} 
			
		}
		return res;
    }

	/**
	 * 功能：生成实体类主体代码
	 * @param colnames
	 * @param colTypes
	 * @param colSizes
	 * @return
	 */
	private String parse(String[] colnames, String[] colTypes, int[] colSizes,String packageOutPath,String tablename) {
		StringBuffer sb = new StringBuffer();
		
		
		
		sb.append("package " + packageOutPath + ";\r\n");
		sb.append("\r\n");
		//判断是否导入工具包
		if(f_util){
			sb.append("import java.util.Date;\r\n");
		}
		if(f_sql){
			sb.append("import java.sql.*;\r\n");
		}
		//注释部分
		sb.append("   /**\r\n");
		sb.append("    * "+tablename+" \r\n");
		sb.append("    * "+new Date()+" "+this.authorName+"\r\n");
		sb.append("    */ \r\n");
		//实体部分
		sb.append("\r\n\r\npublic class " + Util.initcap(tablename) + "{\r\n");
		processAllAttrs(sb);//属性
		processAllMethod(sb);//get set方法
		sb.append("}\r\n");
		
    	//System.out.println(sb.toString());
		return sb.toString();
	}
	
	/**
	 * 功能：生成所有属性
	 * @param sb
	 */
	private void processAllAttrs(StringBuffer sb) {
		
		for (int i = 0; i < colnames.length; i++) {
			sb.append("\tprivate " + sqlType2JavaType(colTypes[i]) + " " + colnames[i] + ";\r\n");
		}
		
	}

	/**
	 * 功能：生成所有方法
	 * @param sb
	 */
	private void processAllMethod(StringBuffer sb) {
		
		for (int i = 0; i < colnames.length; i++) {
			sb.append("\tpublic void set" + Util.initcap(colnames[i]) + "(" + sqlType2JavaType(colTypes[i]) + " " + 
					colnames[i] + "){\r\n");
			sb.append("\tthis." + colnames[i] + "=" + colnames[i] + ";\r\n");
			sb.append("\t}\r\n");
			sb.append("\tpublic " + sqlType2JavaType(colTypes[i]) + " get" + Util.initcap(colnames[i]) + "(){\r\n");
			sb.append("\t\treturn " + colnames[i] + ";\r\n");
			sb.append("\t}\r\n");
		}
		
	}
	
	

	/**
	 * 功能：获得列的数据类型
	 * @param sqlType
	 * @return
	 */
	private String sqlType2JavaType(String sqlType) {
		
		if(sqlType.equalsIgnoreCase("binary_double")){
			return "double";
		}else if(sqlType.equalsIgnoreCase("binary_float")){
			return "float";
		}else if(sqlType.equalsIgnoreCase("blob")){
			return "byte[]";
		}else if(sqlType.equalsIgnoreCase("blob")){
			return "byte[]";
		}else if(sqlType.equalsIgnoreCase("char") || sqlType.equalsIgnoreCase("nvarchar2") 
				|| sqlType.equalsIgnoreCase("varchar2")){
			return "String";
		}else if(sqlType.equalsIgnoreCase("date") || sqlType.equalsIgnoreCase("timestamp")
				 || sqlType.equalsIgnoreCase("timestamp with local time zone") 
				 || sqlType.equalsIgnoreCase("timestamp with time zone")){
			return "Date";
		}else if(sqlType.equalsIgnoreCase("number")){
			return "int";
		}
		
		return "String";
	}
	
	

}